package cn.edu.qvtu;

import java.util.Random;

public class Array1
{
    //生成n个互不相同的随机数，范围在0到size-1之间
    public static Integer[] differentRandom(int n, int size)
    {
        if (n > size)//限定范围
        {
            System.out.println("n要小于size");;
        }
        Integer[] result = new Integer[n];
        // 使用一个数组来记录每个数字是否已经被选择过
        boolean[] chosen = new boolean[size];
        Random random = new Random();
        for (int i = 0; i < n; i++)
        {
            boolean picked = false;
            int num;
            while (!picked)
            {
                num = random.nextInt(size);//随机生成0~size-1的数
                if (!chosen[num])//判断这个数是否重复过
                {
                    chosen[num] = true;
                    result[i] = num;
                    picked = true;
                }
            }
        }
        return result;
    }
    public static void print(Object[] value)
    {
        StringBuilder val = new StringBuilder();
        for (int i = 0; i < value.length; i++)
        {
            val.append(value[i]);
            if (i != value.length - 1)//每个数之间加空格
            {
                val.append(" ");
            }
        }
        System.out.println(val.toString());
    }
}